var utils = require("./utils");
var constant = require("./constant");
var ad_util = {
  sleep_and_log(duration) {
    var interval = 5;
    while (duration > interval) {
      if (utils.exists_text_starts_with("已领取")) {
        var after_interval = random(3, 6);
        console.log(
          "找到【已领取】,再过后" + after_interval + "s后停止当前广告"
        );
        sleep(after_interval * 1000);
        return;
      }
      if (utils.exists_text_starts_with("已完成任务")) {
        var after_interval = random(3, 6);
        console.log(
          "找到【已完成任务】,再过后" + after_interval + "s后停止当前广告"
        );
        sleep(after_interval * 1000);
        return;
      }
      sleep(interval * 1000);
      duration = duration - interval;
      console.log("还剩:" + duration + "s");
    }
    if (duration > 0) {
      sleep(duration * 1000);
      console.log("sleep_and_log: duration ", duration, "s");
    }
  },
  run() {
    var ad_interval = constant.min_ad_time + random(1, 3);
    this.sleep_and_log(ad_interval);
    console.log("ad_util execute: end");
    this.go_back();
    this.after();
  },
  execute() {
    console.show();
    sleep(1 * 1000);
    this.run();
  },
  go_back() {
    var index = 0;
    while (true) {
      if (index > 3) {
        return;
      }
      back();
      sleep(constant.min_click_after_time * 1000);
      if (utils.exists_text_starts_with("跳过")) {
        return;
      }
      if (utils.exists_text_starts_with("立即收下")) {
        return;
      }
      if (utils.exists_text_starts_with("开心收下")) {
        return;
      }
      if (utils.exists_text_starts_with("看视频再得")) {
        return;
      }
      if (utils.exists_text_starts_with("奖励已领取")) {
        return;
      }
      if (utils.exists_text_starts_with("领取成功")) {
        return;
      }
      if (utils.exists_text_starts_with("再看1个获得")) {
        return;
      }
      if (
        utils.exists_text_starts_with("百度") &&
        utils.exists_text_starts_with("视频") &&
        utils.exists_text_starts_with("小说")
      ) {
        console.log("go_back 找到【百度】【视频】【小说】");
        return;
      }
      if (utils.exists_text_starts_with("面对面")) {
        console.log("go_back 找到【面对面】");
        return;
      }
      if (utils.exists_text_starts_with("拒绝")) {
        utils.click_text("拒绝");
        console.log("go_back 找到【拒绝】");
        sleep(constant.min_click_after_time * 1000);
        back();
        sleep(constant.min_click_after_time * 1000);
      }
      index++;
      console.log("go_back index: ", index);
    }
  },
  after() {
    sleep(constant.min_ad_after_time * 1000);
    var ui_text_1 = utils.exists_text_starts_with("看视频再得");
    if (ui_text_1) {
      console.log("gold_video after: 找到【看视频再得】");
      utils.click_text_starts_with("看视频再得");
      sleep(constant.min_click_after_time * 1000);
      this.run();
      return;
    }
    var ui_text_2 = utils.exists_text_starts_with("再看一个领取");
    if (ui_text_2) {
      console.log("ad_util after: 找到【再看一个领取】" + ui_text_2.text());
      var gold = utils.get_gold(ui_text_2.text());
      console.log("ad_util after: " + gold + "金币");
      utils.click_text_starts_with("再看一个领取");
      sleep(constant.min_click_after_time * 1000);
      this.run();
      return;
    }
    var ui_text_3 = utils.exists_text_starts_with("再看1个获得");
    if (ui_text_3) {
      console.log("ad_util after: 找到【再看1个获得】");
      utils.click_text_starts_with("再看1个获得");
      sleep(constant.min_click_after_time * 1000);
      this.run();
      return;
    }
    console.log(
      "ad_util after: 没找到【再看一个领取】或【再看1个获得】或【看视频再得】"
    );

    if (utils.exists_text_starts_with("跳过")) {
      utils.click_text_starts_with("跳过");
      sleep(constant.min_click_after_time * 1000);
    }
    this.next();
  },
  next() {
    console.hide();
    sleep(1 * 1000);
    console.log("ad_util next");
  },
};

module.exports=ad_util